package com.sinosoft.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.frame.service.MailEngine;
import com.sinosoft.user.User;

@Controller
@RequestMapping("/util/passwordSender.html")
public class PwSenderController extends BaseFormController {
	protected MailEngine mailEngine = null;
	protected SimpleMailMessage message = null;
	private MessageSource messageSource = null;

	public PwSenderController() {
		setCancelView("redirect:/util/passwordSender.html");
		setSuccessView("redirect:/util/passwordSender.html");
	}

	@Autowired
	public void setMailEngine(MailEngine mailEngine) {
		this.mailEngine = mailEngine;
	}

	@Autowired
	public void setMessage(SimpleMailMessage message) {
		this.message = message;
	}

	@Autowired
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	@RequestMapping(method = RequestMethod.POST)
	public String handleRequest(User user, String state,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		MessageSourceAccessor text = new MessageSourceAccessor(messageSource,
				request.getLocale());

		if (state.equalsIgnoreCase("in")) {

		} else if (state.equalsIgnoreCase("set")) {
			int isuint = getUserManager().isUnique(user.getRealname(),
					user.getEmail());
			if (isuint >= 1) {

				// 修改密码
				User users = getUserManager().getUserByUsername(
						user.getRealname());
				int i = (int) (Math.random() * 1000000);
				String str = String.valueOf(i);
				String password = new Sha256Hash(str).toHex();
				users.setPassword(password);
				
				
				
				// 发送邮件
				StringBuffer msg = new StringBuffer();
				String newpassword = text.getMessage("new.password");
				msg.append(newpassword + ": " + i);
				message.setTo(users.getEmail());
				String subject = '[' + text.getMessage("webapp.name") + "] "
						+ text.getMessage("user.passwordHint");
				message.setSubject(subject);
				message.setText(msg.toString());
				try {
					mailEngine.send(message);
				} catch (Exception e) {
					saveMessage(request, text.getMessage("password.sender.error"));
					return getCancelView();
				}

				
				getUserManager().saveOrUpdateUser(users);

				saveMessage(request, text.getMessage("login.passwordHint.sent",
						new Object[] { user.getRealname(), user.getEmail() }));

			} else {

				saveError(request, text.getMessage("login.passwordHint.error",
						new Object[] { user.getRealname(), user.getEmail() }));
			}

		}

		return getCancelView();
	}

	@ModelAttribute("emailSender")
	@RequestMapping(method = RequestMethod.GET)
	protected User showForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user = new User();
		return user;
	}

}
